package cn.coderdream.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

public class ClockView extends View {
    private Context mContext;
    private Paint mPaint;
    private int mColorLong = Color.parseColor("#000000");
    private int mColorShort = Color.parseColor("#666666");
    private long time = 0;

    public ClockView(Context context) {
        this(context, null);
    }

    public ClockView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {

        initPaint();

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.ClockView, defStyleAttr, 0);
        try {

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            typedArray.recycle();
        }
    }

    private void initPaint() {
        mPaint = new Paint();
        //设置填充样式
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        //设置抗锯齿
        mPaint.setAntiAlias(true);
        //设置边界模糊
        mPaint.setDither(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1.先换个背景圆
        drawBackground(canvas);
        //2.画表盘
        drawCalibration(canvas);
        //3.画指针
        drawPoint(canvas);


        time++;
        postInvalidateDelayed(1000);
    }

    private void drawPoint(Canvas canvas) {

        float mRadius = ((float) getWidth()) / 2;

        //1.画时针
        canvas.rotate(((float) time / 120));
        mPaint.setStrokeWidth(dp2px(3f));
        mPaint.setColor(Color.parseColor("#333333"));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawLine(0, 60, 0, -(mRadius - 220), mPaint);
        //2.画分针
        canvas.rotate(((float) 0.1 * time) - ((float) time / 120));
        mPaint.setStrokeWidth(dp2px(2f));
        mPaint.setColor(Color.parseColor("#330000"));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawLine(0, 60, 0, -(mRadius - 160), mPaint);
        //3.画秒针
        canvas.rotate((float) (6 * time) - ((float) 0.1 * time));
        mPaint.setStrokeWidth(dp2px(1.2f));
        mPaint.setColor(Color.parseColor("#006600"));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawLine(0, 60, 0, -(mRadius - 100), mPaint);

        canvas.drawCircle(0, 0, 10, mPaint);
    }

    private void drawBackground(Canvas canvas) {
        float width = ((float) getWidth());
        float height = ((float) getHeight());
        canvas.save();
        canvas.translate(0f, 0f);
        RectF rectF = new RectF();
        rectF.left = 0;
        rectF.top = 0;
        rectF.right = width;
        rectF.bottom = height;
        mPaint.setColor(Color.parseColor("#33ff0000"));
        mPaint.setStyle(Paint.Style.FILL);

        canvas.drawCircle(width / 2, height / 2, width / 2, mPaint);
    }

    /**
     * 绘制刻度
     */
    private void drawCalibration(Canvas canvas) {
        mPaint.setStrokeWidth(dp2px(1.6f));

        float mRadius = ((float) getWidth()) / 2;
        float mPadding = 10;
        int lineWith;
        canvas.translate(mRadius, mRadius);

        for (int i = 0; i < 60; i++) {
            //绘制时间
            String timeContent = i + "";
            if (i % 5 == 0) { // 整点
                if (i == 0) {
                    timeContent = "12";
                } else {
                    timeContent = i / 5 + "";
                }
                //整点 刻度稍长，稍粗
                lineWith = 40;
                mPaint.setColor(mColorLong);
                mPaint.setStrokeWidth(dp2px(1.5f));
                mPaint.setTextSize(sp2px(10));
                Rect rectTime = new Rect();
                mPaint.getTextBounds(timeContent, 0, timeContent.length(), rectTime);
                mPaint.setColor(Color.BLACK);
                canvas.save();
                canvas.translate(0, -mRadius + dp2px(5) + lineWith + mPadding + ((float) (rectTime.bottom - rectTime.top)) / 2);
                mPaint.setStyle(Paint.Style.FILL);
                canvas.rotate(-6 * i);
                canvas.drawText(
                        timeContent,
                        -((float) (rectTime.right + rectTime.left)) / 2,
                        -((float) (rectTime.bottom + rectTime.top)) / 2,
                        mPaint);
                canvas.restore();
            } else {
                lineWith = 30;
                mPaint.setColor(mColorShort);
                mPaint.setStrokeWidth(dp2px(1f));
//                mPaint.setTextSize(sp2px(6));
//                Rect rectTime = new Rect();
//                mPaint.getTextBounds(timeContent, 0, timeContent.length(), rectTime);
//                mPaint.setColor(Color.BLACK);
//                canvas.save();
//                canvas.translate(0, -mRadius + dp2px(5) + lineWith + mPadding + ((float) (rectTime.bottom - rectTime.top)) / 2);
//                mPaint.setStyle(Paint.Style.FILL);
//                canvas.rotate(-6 * i);
//                canvas.drawText(
//                        timeContent,
//                        -((float) (rectTime.right + rectTime.left)) / 2,
//                        -((float) (rectTime.bottom + rectTime.top)) / 2,
//                        mPaint);
//                canvas.restore();
            }

            canvas.drawLine(0, -mRadius + mPadding, 0, -mRadius + mPadding + lineWith, mPaint);
            canvas.rotate(6);
        }

    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     */
    public int sp2px(float spValue) {
        float fontScale = getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 将dp值转换为px值
     */
    public int dp2px(float dpValue) {
        float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5F);
    }
}
